/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.gwt.apps.client.dataanalysis;

import java.util.LinkedList;
import java.util.List;

import net.bioteam.appweb.gwt.apps.client.StaticHolder;
import net.bioteam.appweb.gwt.apps.client.Util.ApplicationCallBack;
import net.bioteam.appweb.gwt.apps.client.Util.GwtRpcDataSource;
import net.bioteam.appweb.gwt.apps.clientandserver.dataanalysis.DataAnalysisAction;
import net.bioteam.appweb.gwt.apps.clientandserver.dataanalysis.DataAnalysisActionAsync;
import net.bioteam.appweb.gwt.apps.clientandserver.dataanalysis.TypeNode;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.fields.DataSourceBooleanField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.rpc.RPCResponse;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.tree.TreeNode;

public class UsedTypeDataSource extends GwtRpcDataSource
{


	//public TreeGrid treeGrid;

	public UsedTypeDataSource()
	{

		super();

		
		DataSourceTextField nameField = new DataSourceTextField("name");
		nameField.setPrimaryKey(true);
		DataSourceTextField descField = new DataSourceTextField("description");
		DataSourceBooleanField folderField = new DataSourceBooleanField("folder");
		DataSourceTextField parentField=new DataSourceTextField("parent");
		parentField.setForeignKey("name");
		setFields(nameField, descField, folderField, parentField);

	}

	public void copyValues(TypeNode from, TreeNode record)
	{
		record.setAttribute("name", from.name);
		record.setAttribute("description", from.description);
		record.setAttribute("folder", from.nodes!=null && from.nodes.length>0);
	}
	
	

	@Override
	protected void executeAdd(final String requestId, final DSRequest request, final DSResponse response)
	{
		//this is not supported
		response.setStatus(RPCResponse.STATUS_FAILURE);
		processResponse(requestId, response);

	}

	

	private TreeNode typeNodeToRecord(TypeNode tn, List<TreeNode> rs)
	{
		TreeNode t=new TreeNode();
		copyValues(tn, t);
		rs.add(t);

		if (tn.nodes!=null && tn.nodes.length>0)
		{
			for (TypeNode n: tn.nodes)
			{
				TreeNode t1=typeNodeToRecord(n, rs);
				t1.setParentID(tn.name);
				t1.setAttribute("parent", tn.name);
				
			}
		}
		return t;
	}

	@Override
	protected void executeFetch(final String requestId, final DSRequest request, final DSResponse response)
	{

		JavaScriptObject obj = request.getAttributeAsJavaScriptObject("parentNode");
		TreeNode parentNode = new TreeNode(obj);
		
		final String parentID = parentNode.getAttribute("name");
		if(parentID.equals("Object"))
		{
			ApplicationCallBack<TypeNode[]> callback = new ApplicationCallBack<TypeNode[]>()
			{

				public void onFailureAfter(Throwable caught)
				{
					response.setStatus(RPCResponse.STATUS_FAILURE);
					processResponse(requestId, response);
					SC.warn(caught.getMessage(), StaticHolder.dumpB);

				}

				public void onSuccessAfter(TypeNode[] results)
				{
					List<TreeNode> nodes=new LinkedList<TreeNode>();
					for (TypeNode tn: results)
					{
					TreeNode child=typeNodeToRecord(tn, nodes);
					child.setParentID(parentID);	
					child.setAttribute("parent", parentID);
					}
					response.setData(nodes.toArray(new TreeNode[0]));
					processResponse(requestId, response);
				}

				public void execute()
				{
					DataAnalysisActionAsync proxy=GWT.create(DataAnalysisAction.class);
					proxy.getUsedType(parentID, this);
				}

			};
			callback.execute();
		} else
		{
			response.setData(new TreeNode[0]);
			processResponse(requestId, response);	
		}

		
	}

	@Override
	protected void executeRemove(final String requestId, final DSRequest request, final DSResponse response)
	{
		//this is not supported
		response.setStatus(RPCResponse.STATUS_FAILURE);
		processResponse(requestId, response);
	}


	@Override
	protected void executeUpdate(final String requestId, final DSRequest request, final DSResponse response)
	{
		//this is not supported
		response.setStatus(RPCResponse.STATUS_FAILURE);
		processResponse(requestId, response);
	}
	
	
	
}
